home *** CD-ROM | disk | FTP | other *** search
/ isnet Internet / Isnet Internet CD.iso / prog / hiz / 09 / 09.exe / adynware.exe / perl / lib / diagnostics.pm < prev    next >
Encoding:
Perl POD Document  |  1999-12-28  |  13.0 KB  |  508 lines

  1. package diagnostics;
  2.  
  3. =head1 NAME
  4.  
  5. diagnostics - Perl compiler pragma to force verbose warning diagnostics
  6.  
  7. splain - standalone program to do the same thing
  8.  
  9. =head1 SYNOPSIS
  10.  
  11. As a pragma:
  12.  
  13.     use diagnostics;
  14.     use diagnostics -verbose;
  15.  
  16.     enable  diagnostics;
  17.     disable diagnostics;
  18.  
  19. Aa a program:
  20.  
  21.     perl program 2>diag.out
  22.     splain [-v] [-p] diag.out
  23.  
  24.  
  25. =head1 DESCRIPTION
  26.  
  27. =head2 The C<diagnostics> Pragma
  28.  
  29. This module extends the terse diagnostics normally emitted by both the
  30. perl compiler and the perl interpeter, augmenting them with the more
  31. explicative and endearing descriptions found in L<perldiag>.  Like the
  32. other pragmata, it affects the compilation phase of your program rather
  33. than merely the execution phase.
  34.  
  35. To use in your program as a pragma, merely invoke
  36.  
  37.     use diagnostics;
  38.  
  39. at the start (or near the start) of your program.  (Note 
  40. that this I<does> enable perl's B<-w> flag.)  Your whole
  41. compilation will then be subject(ed :-) to the enhanced diagnostics.
  42. These still go out B<STDERR>.
  43.  
  44. Due to the interaction between runtime and compiletime issues,
  45. and because it's probably not a very good idea anyway,
  46. you may not use C<no diagnostics> to turn them off at compiletime.
  47. However, you may control there behaviour at runtime using the 
  48. disable() and enable() methods to turn them off and on respectively.
  49.  
  50. The B<-verbose> flag first prints out the L<perldiag> introduction before
  51. any other diagnostics.  The $diagnostics::PRETTY variable can generate nicer
  52. escape sequences for pagers.
  53.  
  54. =head2 The I<splain> Program
  55.  
  56. While apparently a whole nuther program, I<splain> is actually nothing
  57. more than a link to the (executable) F<diagnostics.pm> module, as well as
  58. a link to the F<diagnostics.pod> documentation.  The B<-v> flag is like
  59. the C<use diagnostics -verbose> directive.
  60. The B<-p> flag is like the
  61. $diagnostics::PRETTY variable.  Since you're post-processing with 
  62. I<splain>, there's no sense in being able to enable() or disable() processing.
  63.  
  64. Output from I<splain> is directed to B<STDOUT>, unlike the pragma.
  65.  
  66. =head1 EXAMPLES
  67.  
  68. The following file is certain to trigger a few errors at both
  69. runtime and compiletime:
  70.  
  71.     use diagnostics;
  72.     print NOWHERE "nothing\n";
  73.     print STDERR "\n\tThis message should be unadorned.\n";
  74.     warn "\tThis is a user warning";
  75.     print "\nDIAGNOSTIC TESTER: Please enter a <CR> here: ";
  76.     my $a, $b = scalar <STDIN>;
  77.     print "\n";
  78.     print $x/$y;
  79.  
  80. If you prefer to run your program first and look at its problem
  81. afterwards, do this:
  82.  
  83.     perl -w test.pl 2>test.out
  84.     ./splain < test.out
  85.  
  86. Note that this is not in general possible in shells of more dubious heritage, 
  87. as the theoretical 
  88.  
  89.     (perl -w test.pl >/dev/tty) >& test.out
  90.     ./splain < test.out
  91.  
  92. Because you just moved the existing B<stdout> to somewhere else.
  93.  
  94. If you don't want to modify your source code, but still have on-the-fly
  95. warnings, do this:
  96.  
  97.     exec 3>&1; perl -w test.pl 2>&1 1>&3 3>&- | splain 1>&2 3>&- 
  98.  
  99. Nifty, eh?
  100.  
  101. If you want to control warnings on the fly, do something like this.
  102. Make sure you do the C<use> first, or you won't be able to get
  103. at the enable() or disable() methods.
  104.  
  105.     use diagnostics; # checks entire compilation phase 
  106.     print "\ntime for 1st bogus diags: SQUAWKINGS\n";
  107.     print BOGUS1 'nada';
  108.     print "done with 1st bogus\n";
  109.  
  110.     disable diagnostics; # only turns off runtime warnings
  111.     print "\ntime for 2nd bogus: (squelched)\n";
  112.     print BOGUS2 'nada';
  113.     print "done with 2nd bogus\n";
  114.  
  115.     enable diagnostics; # turns back on runtime warnings
  116.     print "\ntime for 3rd bogus: SQUAWKINGS\n";
  117.     print BOGUS3 'nada';
  118.     print "done with 3rd bogus\n";
  119.  
  120.     disable diagnostics;
  121.     print "\ntime for 4th bogus: (squelched)\n";
  122.     print BOGUS4 'nada';
  123.     print "done with 4th bogus\n";
  124.  
  125. =head1 INTERNALS
  126.  
  127. Diagnostic messages derive from the F<perldiag.pod> file when available at
  128. runtime.  Otherwise, they may be embedded in the file itself when the
  129. splain package is built.   See the F<Makefile> for details.
  130.  
  131. If an extant $SIG{__WARN__} handler is discovered, it will continue
  132. to be honored, but only after the diagnostics::splainthis() function 
  133. (the module's $SIG{__WARN__} interceptor) has had its way with your
  134. warnings.
  135.  
  136. There is a $diagnostics::DEBUG variable you may set if you're desperately
  137. curious what sorts of things are being intercepted.
  138.  
  139.     BEGIN { $diagnostics::DEBUG = 1 } 
  140.  
  141.  
  142. =head1 BUGS
  143.  
  144. Not being able to say "no diagnostics" is annoying, but may not be
  145. insurmountable.
  146.  
  147. The C<-pretty> directive is called too late to affect matters.
  148. You have to do this instead, and I<before> you load the module.
  149.  
  150.     BEGIN { $diagnostics::PRETTY = 1 } 
  151.  
  152. I could start up faster by delaying compilation until it should be
  153. needed, but this gets a "panic: top_level" when using the pragma form
  154. in Perl 5.001e.
  155.  
  156. While it's true that this documentation is somewhat subserious, if you use
  157. a program named I<splain>, you should expect a bit of whimsy.
  158.  
  159. =head1 AUTHOR
  160.  
  161. Tom Christiansen <F<tchrist@mox.perl.com>>, 25 June 1995.
  162.  
  163. =cut
  164.  
  165. require 5.001;
  166. use Carp;
  167.  
  168. use Config;
  169. ($privlib, $archlib) = @Config{qw(privlibexp archlibexp)};
  170. if ($^O eq 'VMS') {
  171.     require VMS::Filespec;
  172.     $privlib = VMS::Filespec::unixify($privlib);
  173.     $archlib = VMS::Filespec::unixify($archlib);
  174. }
  175. @trypod = ("$archlib/pod/perldiag.pod",
  176.        "$privlib/pod/perldiag-$].pod",
  177.        "$privlib/pod/perldiag.pod");
  178. ($PODFILE) = ((grep { -e } @trypod), $trypod[$#trypod])[0];
  179.  
  180. $DEBUG ||= 0;
  181. my $WHOAMI = ref bless [];  # nobody's business, prolly not even mine
  182.  
  183. $| = 1;
  184.  
  185. local $_;
  186.  
  187. CONFIG: {
  188.     $opt_p = $opt_d = $opt_v = $opt_f = '';
  189.     %HTML_2_Troff = %HTML_2_Latin_1 = %HTML_2_ASCII_7 = ();  
  190.     %exact_duplicate = ();
  191.  
  192.     unless (caller) { 
  193.     $standalone++;
  194.     require Getopt::Std;
  195.     Getopt::Std::getopts('pdvf:')
  196.         or die "Usage: $0 [-v] [-p] [-f splainpod]";
  197.     $PODFILE = $opt_f if $opt_f;
  198.     $DEBUG = 2 if $opt_d;
  199.     $VERBOSE = $opt_v;
  200.     $PRETTY = $opt_p;
  201.     } 
  202.  
  203.     if (open(POD_DIAG, $PODFILE)) {
  204.     warn "Happy happy podfile from real $PODFILE\n" if $DEBUG;
  205.     last CONFIG;
  206.     } 
  207.  
  208.     if (caller) {
  209.     INCPATH: {
  210.         for $file ( (map { "$_/$WHOAMI.pm" } @INC), $0) {
  211.         warn "Checking $file\n" if $DEBUG;
  212.         if (open(POD_DIAG, $file)) {
  213.             while (<POD_DIAG>) {
  214.             next unless /^__END__\s*# wish diag dbase were more accessible/;
  215.             print STDERR "podfile is $file\n" if $DEBUG;
  216.             last INCPATH;
  217.             }
  218.         }
  219.         } 
  220.     }
  221.     } else { 
  222.     print STDERR "podfile is <DATA>\n" if $DEBUG;
  223.     *POD_DIAG = *main::DATA;
  224.     }
  225. }
  226. if (eof(POD_DIAG)) { 
  227.     die "couldn't find diagnostic data in $PODFILE @INC $0";
  228. }
  229.  
  230.  
  231. %HTML_2_Troff = (
  232.     'amp'    =>    '&',    #   ampersand
  233.     'lt'    =>    '<',    #   left chevron, less-than
  234.     'gt'    =>    '>',    #   right chevron, greater-than
  235.     'quot'    =>    '"',    #   double quote
  236.  
  237.     "Aacute"    =>    "A\\*'",    #   capital A, acute accent
  238.  
  239. );
  240.  
  241. %HTML_2_Latin_1 = (
  242.     'amp'    =>    '&',    #   ampersand
  243.     'lt'    =>    '<',    #   left chevron, less-than
  244.     'gt'    =>    '>',    #   right chevron, greater-than
  245.     'quot'    =>    '"',    #   double quote
  246.  
  247.     "Aacute"    =>    "\xC1"    #   capital A, acute accent
  248.  
  249. );
  250.  
  251. %HTML_2_ASCII_7 = (
  252.     'amp'    =>    '&',    #   ampersand
  253.     'lt'    =>    '<',    #   left chevron, less-than
  254.     'gt'    =>    '>',    #   right chevron, greater-than
  255.     'quot'    =>    '"',    #   double quote
  256.  
  257.     "Aacute"    =>    "A"    #   capital A, acute accent
  258. );
  259.  
  260. *HTML_Escapes = do {
  261.     if ($standalone) {
  262.     $PRETTY ? \%HTML_2_Latin_1 : \%HTML_2_ASCII_7; 
  263.     } else {
  264.     \%HTML_2_Latin_1; 
  265.     }
  266. }; 
  267.  
  268. *THITHER = $standalone ? *STDOUT : *STDERR;
  269.  
  270. $transmo = <<EOFUNC;
  271. sub transmo {
  272.     local \$^W = 0;  # recursive warnings we do NOT need!
  273.     study;
  274. EOFUNC
  275.  
  276.     print STDERR "FINISHING COMPILATION for $_\n" if $DEBUG;
  277.     $RS = '';
  278.     local $_;
  279.     while (<POD_DIAG>) {
  280.  
  281.     unescape();
  282.     if ($PRETTY) {
  283.         sub noop   { return $_[0] }  # spensive for a noop
  284.         sub bold   { my $str =$_[0];  $str =~ s/(.)/$1\b$1/g; return $str; } 
  285.         sub italic { my $str = $_[0]; $str =~ s/(.)/_\b$1/g;  return $str; } 
  286.         s/[BC]<(.*?)>/bold($1)/ges;
  287.         s/[LIF]<(.*?)>/italic($1)/ges;
  288.     } else {
  289.         s/[BC]<(.*?)>/$1/gs;
  290.         s/[LIF]<(.*?)>/$1/gs;
  291.     } 
  292.     unless (/^=/) {
  293.         if (defined $header) { 
  294.         if ( $header eq 'DESCRIPTION' && 
  295.             (   /Optional warnings are enabled/ 
  296.              || /Some of these messages are generic./
  297.             ) )
  298.         {
  299.             next;
  300.         } 
  301.         s/^/    /gm;
  302.         $msg{$header} .= $_;
  303.         }
  304.         next;
  305.     } 
  306.     unless ( s/=item (.*)\s*\Z//) {
  307.  
  308.         if ( s/=head1\sDESCRIPTION//) {
  309.         $msg{$header = 'DESCRIPTION'} = '';
  310.         }
  311.         next;
  312.     }
  313.  
  314.     ($header = $1) =~ s/[A-Z]<(.*?)>/$1/g;
  315.  
  316.     if ($header =~ /%[sd]/) {
  317.         $rhs = $lhs = $header;
  318.         if ($lhs =~ s/(.*?)%d(?!%d)(.*)/\Q$1\E\\d+\Q$2\E/g)  {
  319.         $lhs =~ s/\\%s/.*?/g;
  320.         } else {
  321.         $lhs =~ s/(.*?)%s/\Q$1\E.*?\377/g;
  322.         $lhs =~ s/\377([^\377]*)$/\Q$1\E/;
  323.         $lhs =~ s/\377//g;
  324.         $lhs =~ s/\.\*\?$/.*/; # Allow %s at the end to eat it all
  325.         } 
  326.         $transmo .= "    s{^$lhs}\n     {\Q$rhs\E}s\n\t&& return 1;\n";
  327.     } else {
  328.         $transmo .= "    m{^\Q$header\E} && return 1;\n";
  329.     } 
  330.  
  331.     print STDERR "$WHOAMI: Duplicate entry: \"$header\"\n"
  332.         if $msg{$header};
  333.  
  334.     $msg{$header} = '';
  335.     } 
  336.  
  337.  
  338.     close POD_DIAG unless *main::DATA eq *POD_DIAG;
  339.  
  340.     die "No diagnostics?" unless %msg;
  341.  
  342.     $transmo .= "    return 0;\n}\n";
  343.     print STDERR $transmo if $DEBUG;
  344.     eval $transmo;
  345.     die $@ if $@;
  346.     $RS = "\n";
  347.  
  348. if ($standalone) {
  349.     if (!@ARGV and -t STDIN) { print STDERR "$0: Reading from STDIN\n" } 
  350.     while (defined ($error = <>)) {
  351.     splainthis($error) || print THITHER $error;
  352.     } 
  353.     exit;
  354. } else { 
  355.     $old_w = 0; $oldwarn = ''; $olddie = '';
  356. }
  357.  
  358. sub import {
  359.     shift;
  360.     $old_w = $^W;
  361.     $^W = 1; # yup, clobbered the global variable; tough, if you
  362.     return if $SIG{__WARN__} eq \&warn_trap;
  363.  
  364.     for (@_) {
  365.  
  366.     /^-d(ebug)?$/            && do {
  367.                     $DEBUG++;
  368.                     next;
  369.                    };
  370.  
  371.     /^-v(erbose)?$/     && do {
  372.                     $VERBOSE++;
  373.                     next;
  374.                    };
  375.  
  376.     /^-p(retty)?$/         && do {
  377.                     print STDERR "$0: I'm afraid it's too late for prettiness.\n";
  378.                     $PRETTY++;
  379.                     next;
  380.                    };
  381.  
  382.     warn "Unknown flag: $_";
  383.     } 
  384.  
  385.     $oldwarn = $SIG{__WARN__};
  386.     $olddie = $SIG{__DIE__};
  387.     $SIG{__WARN__} = \&warn_trap;
  388.     $SIG{__DIE__} = \&death_trap;
  389.  
  390. sub enable { &import }
  391.  
  392. sub disable {
  393.     shift;
  394.     $^W = $old_w;
  395.     return unless $SIG{__WARN__} eq \&warn_trap;
  396.     $SIG{__WARN__} = $oldwarn;
  397.     $SIG{__DIE__} = $olddie;
  398.  
  399. sub warn_trap {
  400.     my $warning = $_[0];
  401.     if (caller eq $WHOAMI or !splainthis($warning)) {
  402.     print STDERR $warning;
  403.     } 
  404.     &$oldwarn if defined $oldwarn and $oldwarn and $oldwarn ne \&warn_trap;
  405. };
  406.  
  407. sub death_trap {
  408.     my $exception = $_[0];
  409.  
  410.     my $in_eval = 0;
  411.     my $i = 0;
  412.     while (1) {
  413.       my $caller = (caller($i++))[3] or last;
  414.       if ($caller eq '(eval)') {
  415.     $in_eval = 1;
  416.     last;
  417.       }
  418.     }
  419.  
  420.     splainthis($exception) unless $in_eval;
  421.     if (caller eq $WHOAMI) { print STDERR "INTERNAL EXCEPTION: $exception"; } 
  422.     &$olddie if defined $olddie and $olddie and $olddie ne \&death_trap;
  423.  
  424.     $SIG{__DIE__} = $SIG{__WARN__} = '' unless $in_eval;
  425.     local($Carp::CarpLevel) = 1;
  426.     confess "Uncaught exception from user code:\n\t$exception";
  427. };
  428.  
  429. sub splainthis {
  430.     local $_ = shift;
  431.     local $\;
  432.     s/\.?\n+$//;
  433.     my $orig = $_;
  434.     if ($exact_duplicate{$_}++) {
  435.     return 1;
  436.     } 
  437.     s/, <.*?> (?:line|chunk).*$//;
  438.     $real = s/(.*?) at .*? (?:line|chunk) \d+.*/$1/;
  439.     s/^\((.*)\)$/$1/;
  440.     return 0 unless &transmo;
  441.     $orig = shorten($orig);
  442.     if ($old_diag{$_}) {
  443.     autodescribe();
  444.     print THITHER "$orig (#$old_diag{$_})\n";
  445.     $wantspace = 1;
  446.     } else {
  447.     autodescribe();
  448.     $old_diag{$_} = ++$count;
  449.     print THITHER "\n" if $wantspace;
  450.     $wantspace = 0;
  451.     print THITHER "$orig (#$old_diag{$_})\n";
  452.     if ($msg{$_}) {
  453.         print THITHER $msg{$_};
  454.     } else {
  455.         if (0 and $standalone) { 
  456.         print THITHER "    **** Error #$old_diag{$_} ",
  457.             ($real ? "is" : "appears to be"),
  458.             " an unknown diagnostic message.\n\n";
  459.         }
  460.         return 0;
  461.     } 
  462.     }
  463.     return 1;
  464.  
  465. sub autodescribe {
  466.     if ($VERBOSE and not $count) {
  467.     print THITHER &{$PRETTY ? \&bold : \&noop}("DESCRIPTION OF DIAGNOSTICS"),
  468.         "\n$msg{DESCRIPTION}\n";
  469.     } 
  470.  
  471. sub unescape { 
  472.     s {
  473.             E<  
  474.             ( [A-Za-z]+ )       
  475.             >   
  476.     } { 
  477.          do {   
  478.              exists $HTML_Escapes{$1}
  479.                 ? do { $HTML_Escapes{$1} }
  480.                 : do {
  481.                     warn "Unknown escape: E<$1> in $_";
  482.                     "E<$1>";
  483.                 } 
  484.          } 
  485.     }egx;
  486. }
  487.  
  488. sub shorten {
  489.     my $line = $_[0];
  490.     if (length($line) > 79 and index($line, "\n") == -1) {
  491.     my $space_place = rindex($line, ' ', 79);
  492.     if ($space_place != -1) {
  493.         substr($line, $space_place, 1) = "\n\t";
  494.     } 
  495.     } 
  496.     return $line;
  497.  
  498.  
  499. $RS = "\n";
  500.  
  501. 1 unless $standalone;  # or it'll complain about itself
  502. __END__ # wish diag dbase were more accessible
  503.